/*==============================================================================
FILE NAME: Figure_A10.do
CREATED: 22 July 2025
==============================================================================*/

// Figure A10 


/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/

use "$processed_data/Air_Panel.dta", clear

drop if never_air_inv == 1

egen RN_year = group(RN year)
egen t = group(year month)

keep RN_id t RN_year region_id day_air_complaint_inv day_air_nocomplaint_inv incident other_air_complaint_region SIC_2digit1 ind_air_complaint

xtset RN_id t

cap drop b u d se Years Zero
gen Years = _n-13 if _n<=37
gen Zero = 0 if _n <=37
gen b = 0
gen se = 0
gen u = 0 
gen d = 0
** facility-level aggregation:

xtset RN_id t
foreach y in day_air_complaint_inv day_air_nocomplaint_inv {
forv h = 0/12 {
gen `y'_`h' = f`h'.`y' - l1.`y'
}
forv h = 2/12 {
gen `y'_neg`h' = l`h'.`y' - l1.`y'
}
}

// Regressions for number of days of investigaion for noncomplaint investigations
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_nocomplaint_inv_`h' incident other_air_complaint_region, absorb(RN_year t) cluster(region_id)

replace b = _b[other_air_complaint_region]*1394 if Years == `h'
replace se = _se[other_air_complaint_region]*1394 if Years == `h'
replace u = ((_b[other_air_complaint_region]*1394) + 1.96*(_se[other_air_complaint_region]*1394)) if Years == `h'
replace d = ((_b[other_air_complaint_region]*1394) - 1.96*(_se[other_air_complaint_region]*1394)) if Years == `h'
}
	
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_nocomplaint_inv_neg`h' incident other_air_complaint_region, absorb(RN_year t) cluster(region_id)
replace b = _b[other_air_complaint_region]*1394 if Years == -`h'
replace se = _se[other_air_complaint_region]*1394 if Years == -`h'
replace u = ((_b[other_air_complaint_region]*1394) + 1.96*(_se[other_air_complaint_region]*1394)) if Years == -`h'
replace d = ((_b[other_air_complaint_region]*1394) - 1.96*(_se[other_air_complaint_region]*1394)) if Years == -`h'
}	

foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_nocomplaint_inv_`h' incident ind_air_complaint, absorb(RN_year t) cluster(SIC_2digit1)

replace b = _b[ind_air_complaint]*255 if Years == `h'
replace se = _se[ind_air_complaint]*255 if Years == `h'
replace u = ((_b[ind_air_complaint]*255) + 1.96*(_se[ind_air_complaint]*255)) if Years == `h'
replace d = ((_b[ind_air_complaint]*255) - 1.96*(_se[ind_air_complaint]*255)) if Years == `h'
}
	
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_nocomplaint_inv_neg`h' incident ind_air_complaint, absorb(RN_year t) cluster(SIC_2digit1)
replace b = _b[ind_air_complaint]*255 if Years == -`h'
replace se = _se[ind_air_complaint]*255 if Years == -`h'
replace u = ((_b[ind_air_complaint]*255) + 1.96*(_se[ind_air_complaint]*255)) if Years == -`h'
replace d = ((_b[ind_air_complaint]*255) - 1.96*(_se[ind_air_complaint]*255)) if Years == -`h'
}	

preserve
keep if Years != . 
drop if Years > 12
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A10_Panel_A.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(vlarge)) ylabel(-0.4(0.1)0.4, labsize(vlarge)) legend(off) ytitle("{&Delta} # of Non-Complaint" "Investigation Days", size(vlarge)) xtitle("Month", size(vlarge)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)
graph export "$figures/Figure_A10_Panel_A.pdf", replace

restore

// Regressions for number of days of investigation for facilities with complaints
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_complaint_inv_`h' incident ind_air_complaint, absorb(RN_year t) cluster(SIC_2digit1)

replace b = _b[ind_air_complaint]*255 if Years == `h'
replace se = _se[ind_air_complaint]*255 if Years == `h'
replace u = ((_b[ind_air_complaint]*255) + 1.96*(_se[ind_air_complaint]*255)) if Years == `h'
replace d = ((_b[ind_air_complaint]*255) - 1.96*(_se[ind_air_complaint]*255)) if Years == `h'
}
	
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_complaint_inv_neg`h' incident ind_air_complaint, absorb(RN_year t) cluster(SIC_2digit1)
replace b = _b[ind_air_complaint]*255 if Years == -`h'
replace se = _se[ind_air_complaint]*255 if Years == -`h'
replace u = ((_b[ind_air_complaint]*255) + 1.96*(_se[ind_air_complaint]*255)) if Years == -`h'
replace d = ((_b[ind_air_complaint]*255) - 1.96*(_se[ind_air_complaint]*255)) if Years == -`h'
}	

preserve
keep if Years != . 
drop if Years > 12
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A10_Panel_B.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(vlarge)) ylabel(-0.4(0.1)0.4, labsize(vlarge)) legend(off) ytitle("{&Delta} # of Complaint" "Investigation Days", size(vlarge)) xtitle("Month", size(vlarge)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)
graph export "$figures/Figure_A10_Panel_B.pdf", replace

restore